package com.weifei.core.mvc;

import org.omg.CORBA.PUBLIC_MEMBER;

import java.io.File;
import java.util.Locale;

public class WFViewResolver {

    private String DEFAULT_TEMPLATE_SUFFIX = ".html";

    private File templateRootDir;
    private String viewName;

    public WFViewResolver() {}

    public WFViewResolver(String templateRootDir) {
       /* String templateRootPath = this.getClass().getClassLoader()
                .getResource(templateRootDir).getFile();*/
        this.templateRootDir = new File(templateRootDir);
    }
    public void setTemplateSuffix(String suffix) {
        this.DEFAULT_TEMPLATE_SUFFIX = suffix;
    }
    public WFView resolveView(String viewName, Locale locale) {
        this.viewName = viewName;
        if (null == viewName || "".equals(viewName.trim())) {
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX) ? viewName
                : viewName + DEFAULT_TEMPLATE_SUFFIX;
        File templateFile = new File((templateRootDir.getPath() + "/" +viewName)
                .replaceAll("/+", "/"));
        return new WFView(templateFile);
    }

    public String getViewName() {
        return viewName;
    }
}

